//
//  LWShopCarCellTableViewCell.m
//  LWShopItem
//
//  Created by apple on 17/4/15.
//  Copyright © 2017年 LWShop.com. All rights reserved.
//

#import "LWShopCarCellTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "LWLabel.h"
@interface LWShopCarCellTableViewCell()

@property (weak, nonatomic) IBOutlet UIButton *seleBtn;
@property (weak, nonatomic) IBOutlet UIImageView *pictureImage;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
@property (weak, nonatomic) IBOutlet UIButton *reduceBtn;
@property (weak, nonatomic) IBOutlet UIButton *increaseBtn;
@property (weak, nonatomic) IBOutlet UILabel *countlabel;
@property (weak, nonatomic) IBOutlet LWLabel *originalLabel;
@property (weak, nonatomic) IBOutlet UILabel *pricelabel;


@end

@implementation LWShopCarCellTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.seleBtn.layer.borderWidth = 1;
    self.seleBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
}
- (IBAction)changeCountClick:(UIButton *)sender {
    if (sender.tag == 0) {
        if (_model.orderCount > 1) {
            _model.orderCount --;
            _reduceBtn.hidden = NO;
        } else if (_model.orderCount == 1) {
            _model.orderCount --;
            _reduceBtn.hidden = YES;
            _countlabel.hidden = YES;
        }
    }
    if (sender.tag == 1) {
        _model.orderCount ++;
        _reduceBtn.hidden = NO;
        _countlabel.hidden = NO;
    }
    if ([_delegate respondsToSelector:@selector(changeShopCount)]) {
        [_delegate changeShopCount];
    }
    
}

- (IBAction)deleteCLick:(id)sender {
    if ([_delegate respondsToSelector:@selector(deleteFromShopCarWithModel:)]) {
        [_delegate deleteFromShopCarWithModel:_model];
    }
    
}
- (IBAction)selectAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        _model.isSeleted = YES;
        [_seleBtn setImage:[UIImage imageNamed:@"xuanze"] forState:UIControlStateNormal];
    } else {
        _model.isSeleted = NO;
        [_seleBtn setImage:nil forState:UIControlStateNormal];
    }
    if ([_delegate respondsToSelector:@selector(notifyShopCarChangeModelState)]) {
        [_delegate notifyShopCarChangeModelState];
    }
}

#pragma mark - 填充子控件
- (void)setModel:(LWDetailModel *)model {
    _model = model;
    if (_model.isSeleted) {
        [_seleBtn setImage:[UIImage imageNamed:@"xuanze"] forState:UIControlStateNormal];
    } else {
        [_seleBtn setImage:nil forState:UIControlStateNormal];
    }
    [_pictureImage sd_setImageWithURL:[NSURL URLWithString:_model.original_img]];
    _nameLabel.text = _model.goods_name;
    _descLabel.text = @"欢迎购买该商品~";
    if (_model.orderCount > 0) {
        _countlabel.text = @(_model.orderCount).description;
    } else {
        _reduceBtn.hidden = YES;
        _countlabel.hidden = YES;
    }
    
    _originalLabel.text = [NSString stringWithFormat:@"¥%@元",_model.market_price];
    _pricelabel.text = [NSString stringWithFormat:@"¥%@元",_model.shop_price];
}
@end
